<?php
namespace Home\Controller;

/**
 * 预定端客栈相关接口
 * @modify trainyao 2016-05-03 创建
 */

class HotelController extends ParentController
{
    protected function _initialize() {
    }

    /**
     * 获取客栈预定端信息
     * @modify trainyao 2016-05-03    创建
     */
    public function info() {
        $param = I('post.');
        // 调用RPC获取信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel'],
            'bookLangSet' => LANG_SET,
        );
        $result = $client->info($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 是否在下单分割时间段之内
        $isSplitOrderTime = $this->isSplitOrderTime($param['hotel'], date('Y-m-d'));
        $result['data']['isSplitOrderTime'] = $isSplitOrderTime ? true : false;

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取客栈支付方式
     * @modify trainyao 2016-07-12 创建
     */
    public function payInfo()
    {
        $param = I('post.');
        
        // 调用RPC获取信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel']
        );
        $result = $client->payInfo($RPCData);
        if (! is_array($result)) {
            returnJson(500, 500);
            return;
        }

        $areaCode = session(C('USER_AREACODE'));
        $mobile = session(C('USER_MOBILE'));
        $isVipUser = false;
        if (!empty($mobile)) {
            $vipUserModel = D('VipUser');
            $vipUserInfo = $vipUserModel->getInfoByMobile($param['hotel'] ,$mobile, $areaCode);
            if (!empty($vipUserInfo)) {
                $isVipUser = true;
            }
        }
        
        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            $returnData = array();
            $payLogic = A('Common/Pay', 'Logic');
            $payTypeStatus = $payLogic->getPayType($param['hotel'], (int) $result['data']['type']);
            
            $aliPay = false;
            if ($payTypeStatus['uclbrt_alipay']) {
                $aliPay = true;
            } else if ($payTypeStatus['hotel_alipay']) {
                $aliPay = true;
                $aliPay = $aliPay && ! empty($result['data']['partnerPayInfo']['ali']['account']);
                $aliPay = $aliPay && ! empty($result['data']['partnerPayInfo']['ali']['pid']);
                $aliPay = $aliPay && ! empty($result['data']['partnerPayInfo']['ali']['key']);
            }
            $returnData['ali'] = $aliPay ? ENABLE : DISABLE;
            
            $wechatPay = false;
            if ($payTypeStatus['uclbrt_wechat']) {
                $wechatPay = true;
                $returnData['uclbrtWechat'] = ENABLE;
            } else if ($payTypeStatus['hotel_wechat']) {
                $wechatPay = true;                    
                $wechatPay = $wechatPay && ! empty($result['data']['partnerPayInfo']['wechat']['appid']);
                $wechatPay = $wechatPay && ! empty($result['data']['partnerPayInfo']['wechat']['appsecret']);
                $wechatPay = $wechatPay && ! empty($result['data']['partnerPayInfo']['wechat']['mchid']);
                $wechatPay = $wechatPay && ! empty($result['data']['partnerPayInfo']['wechat']['mchkey']);
                $returnData['uclbrtWechat'] = DISABLE;
            }
            $returnData['wechat'] = $wechatPay ? ENABLE : DISABLE;
            // 会员卡
            $returnData['vipcardpay'] = $payTypeStatus['vipcardpay'] && $isVipUser ? ENABLE : DISABLE;
                     
            returnJson(200, 200, $returnData);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取客栈微信支付信息,只有在商户支付,且完善微信支付才会暴露出来
     * @modify trainyao 2016-08-11    创建
     */
    public function wechatPayInfo() {
        $hotelId = I('post.hotel');
        // 获取客栈支付信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $hotelId,
        );
        $payInfoResult = $client->payInfo($RPCData);
        $hasError = false;
        if (!is_array($payInfoResult) || $payInfoResult['httpstatus'] != 200) {
            $hasError = true;
        }        
        
        $payLogic = A('Common/Pay', 'Logic');
        $payTypeStatus = $payLogic->getPayType($hotelId, (int) $payInfoResult['data']['type']);
        
        $returnData = array(
            'ai' => '',
            'asc' => '',
        );
        if (!$hasError && $payTypeStatus['hotel_wechat'] && !empty($payInfoResult['data']['partnerPayInfo']['wechat']['appid']) && !empty($payInfoResult['data']['partnerPayInfo']['wechat']['appsecret'])) {
            $returnData['ai'] = $payInfoResult['data']['partnerPayInfo']['wechat']['appid'];
            $returnData['asc'] = $payInfoResult['data']['partnerPayInfo']['wechat']['appsecret'];
        }

        returnJson(200, 200, $returnData);
    }

    /**
     * 公共推广链接信息
     * @modify trainyao 2016-06-06    创建
     */
    public function publicLink() {
        $param = I('post.');
        $validateRule = array(
            'id' => 'notnull,notempty',
        );
        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 调用RPC获取信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'id' => $param['id'],
        );
        $result = $client->publicLink($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取个性推荐信息
     * @modify trainyao 2016-04-28    创建
     * @modify wenhui 2016-10-28    修改数据结构
     */
    public function special() {

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        $hotelId = $param['hotel'];
        $hotelSpecialLogic = A('Common/Special', 'Logic');
        // 获取hotelspecial表中的数据 组装新的数据格式
        $hotelSpecialDetail = $hotelSpecialLogic->specialInfo($hotelId);
        $bookMeizhuyun = MEIZHU_HOST;
        $hotelDetail = array(
            'minPrice' => isset($hotelSpecialDetail['minPrice']) && !empty($hotelSpecialDetail['minPrice']) ? $hotelSpecialDetail['minPrice'] : '',
            'hotelName' => $hotelSpecialDetail['hotelname'],
        );
        unset($hotelSpecialDetail['minPrice']);
        unset($hotelSpecialDetail['hotelname']);

        $hotelDetailsTiem = array();
        foreach ($hotelSpecialDetail as &$hotelSpecialItems) {
            // 处理数据
            unset($hotelSpecialItems['hotelentity_id']);
            $hotelSpecialItem = array(
                'specialId' => $hotelSpecialItems['id'],
                'special' => $hotelSpecialItems['special'],
                'specialImageUrl' => $bookMeizhuyun.$hotelSpecialItems['specialimageurl'],
                'isactive' => $hotelSpecialItems['isactive'],
                'updateTime' => strtotime($hotelSpecialItems['updatetime']),
                'index' => $hotelSpecialItems['sortindex'],
            );
            array_push($hotelDetailsTiem, $hotelSpecialItem);
        }
        unset($hotelSpecialItems);
        // 排序 按升序
        $hotelDetailsTiem = my_sort($hotelDetailsTiem, 'index', SORT_ASC, SORT_NUMERIC);
        $hotelDetail['specials'] = $hotelDetailsTiem;

        returnJson(200, 200, $hotelDetail);
    }

    /**
     * 获取客栈优惠券
     * @modify wenhui 2018-01-08    创建
     */
    public function getCoupon() {
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        $userId = getUserId();
        // 调用RPC获取信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotelId' => $param['hotel'],
            'userId' => $userId,
            'bookLangSet' => LANG_SET,
        );
        $result = $client->getCoupon($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取客栈优惠券详情
     * @modify wenhui 2018-01-08    创建
     */
    public function couponDetail() {
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'couponNo' => 'notnull,notempty,cpOrder',
        );
        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        $userId = getUserId();
        // 调用RPC获取信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotelId' => $param['hotel'],
            'couponNo' => $param['couponNo'],
        );
        $result = $client->couponDetails($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    public function rpcTest(){
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('SHOP_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('SHOP_RPC_FLAG'),
            'time' => time(),
        );
        $result = $client->shop($RPCData);
        if (!is_array($result) || $result['error'] == 6) {
            returnJson(500, 500);
            return;
        }
        returnJson(200, 200, $result['data']);
    }
}
